package net.jueb.object.client;

import net.jueb.object.pojo.Request;
import net.jueb.object.pojo.Response;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class ObjectClientHandler extends ChannelHandlerAdapter{

	/**
	 * 在连接激活的时候发送请求
	 */
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		for(int i=1;i<=10;i++)
		{
			Request req=new Request();
			req.setId(i);
			req.setMsg("Hello Server!");
			ctx.writeAndFlush(req);
		}
	}
	
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		Response rep=(Response)msg;
		if(rep==null)
		{
			return ;
		}
		System.out.println("收到回复："+rep.toString());
	}
	
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
			throws Exception {
		cause.printStackTrace();
		ctx.close();
	}
}
